package com.geeguo.ebuilder.operation.controller.tenant.datasourceinfo;

import com.geeguo.ebuilder.core.common.action.ActionResult;
import com.geeguo.ebuilder.core.common.model.vo.PageListVO;
import com.geeguo.ebuilder.core.common.utils.key.UUIDCheckUtils;
import com.geeguo.ebuilder.core.common.utils.key.UUIDGenerator;
import com.geeguo.ebuilder.core.security.annotation.RequiresPermissions;
import com.geeguo.ebuilder.operation.base.core.constants.MessageConstants;
import com.geeguo.ebuilder.operation.base.core.dictionary.ClientTypeEnum;
import com.geeguo.ebuilder.operation.base.core.dictionary.LogTypeEnum;
import com.geeguo.ebuilder.operation.base.log.annotation.Log;
import com.geeguo.ebuilder.operation.base.security.model.LoginUser;
import com.geeguo.ebuilder.operation.base.security.utils.AuthUtils;
import com.geeguo.ebuilder.operation.business.tenant.datasourceinfo.model.*;
import com.geeguo.ebuilder.operation.business.tenant.datasourceinfo.service.DataSourceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Tag(name = "数据源信息")
@RestController
public class DataSourceInfoController {
    
    @Autowired
    private DataSourceInfoService dataSourceInfoService;
    
    @Operation(summary = "分页查询数据源信息")
    @PostMapping("/tenant/dataSourceInfo/list")
    @RequiresPermissions("Tenant:DataSourceInfo")
    public ActionResult<PageListVO<DataSourceInfoVO>> list(@RequestBody DataSourceInfoQuery query) {
        return ActionResult.page(dataSourceInfoService.list(query));
    }
    
    @Operation(summary = "保存数据源信息")
    @PostMapping("/tenant/dataSourceInfo/save")
    @RequiresPermissions("Tenant:DataSourceInfo")
    public ActionResult<Boolean> save(@Validated @RequestBody DataSourceInfoCreateDTO dto) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        DataSourceInfoEntity dataSourceInfo = DataSourceInfoConverter.INSTANCE.convert2Entity(dto);
        dataSourceInfo.setId(UUIDGenerator.getId());
        dataSourceInfo.setCreateTime(new Date());
        dataSourceInfo.setCreateUser(currentUser.getUserId());
        if (dataSourceInfoService.save(dataSourceInfo)) {
            return ActionResult.success();
        }
        return ActionResult.fail();
    }
    
    @Operation(summary = "根据ID获取数据源信息")
    @GetMapping("/tenant/dataSourceInfo/get/{id}")
    @RequiresPermissions("Tenant:DataSourceInfo")
    public ActionResult<DataSourceInfoVO> get(@PathVariable("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            DataSourceInfoVO dataSourceInfo = dataSourceInfoService.get(id);
            if (dataSourceInfo != null) {
                return ActionResult.success(dataSourceInfo);
            }
            return ActionResult.fail(MessageConstants.FAIL_DATA_NOT_EXIST);
        }
        return ActionResult.fail(MessageConstants.FAIL_REQUEST_DATA_ERROR);
    }

    @Operation(summary = "更新数据源信息")
    @PostMapping("/tenant/dataSourceInfo/update")
    @RequiresPermissions("Tenant:DataSourceInfo")
    public ActionResult<Boolean> update(@Validated @RequestBody DataSourceInfoUpdateDTO dto) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        DataSourceInfoVO old = dataSourceInfoService.get(dto.getId());
        if (old != null) {
            DataSourceInfoEntity dataSourceInfo = DataSourceInfoConverter.INSTANCE.convert2Entity(dto);
            dataSourceInfo.setModifyTime(new Date());
            dataSourceInfo.setModifyUser(currentUser.getUserId());
            if (dataSourceInfoService.update(dataSourceInfo, DataSourceInfoConverter.INSTANCE.convert2Entity(old))) {
                return ActionResult.success();
            }
        }
        return ActionResult.fail();
    }
    
    @Operation(summary = "删除数据源信息")
    @PostMapping("/tenant/dataSourceInfo/remove")
    @RequiresPermissions("Tenant:DataSourceInfo")
    @Log(title = "删除数据源信息", logType = LogTypeEnum.DELETE, clientType = ClientTypeEnum.Web)
    public ActionResult<Boolean> remove(@RequestParam("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            if (dataSourceInfoService.remove(id)) {
                return ActionResult.success();
            }
        }
        return ActionResult.fail();
    }
}
